package com.example.springbootdemo.controller.rqandrpBody;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@RestController
public class ResponseTest {
    @Resource
    HttpServletRequest request;
    @Resource
    HttpServletResponse response;
    @RequestMapping("responseTest")
    public void responseTest1() throws IOException {
        System.out.println("2222222222");
        // 相当于@ResponseBody加return
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("hahhahh");
    }
    @RequestMapping("responseTest2")
    public void responseTest2() throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().print(JSON.toJSONString(Arrays.asList("aa","bb","cc")));
    }

    @RequestMapping("responseTest3")
    public void responseTest3() throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(JSON.toJSONString(Arrays.asList("aa","bb","cc")));
    }

    @RequestMapping("responseTest4")
    public void responseTest4() throws IOException {
        // 可以直接调出下载框
        response.setHeader("Content-Disposition", "attachment;filename=");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(JSON.toJSONString(Arrays.asList("aa","bb","cc")));
    }

    /**
     * 响应两次会报错
     * @throws IOException
     */
    @RequestMapping("responseTest5")
    @ResponseBody
    public String responseTest5() throws IOException {
        // 可以直接调出下载框
        response.setHeader("Content-Disposition", "attachment;filename=");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(JSON.toJSONString(Arrays.asList("aa","bb","cc")));
        return JSON.toJSONString("aaa");
    }

    @RequestMapping("responseTest6")
    @ResponseBody
    public String responseTest6(@RequestParam MultipartFile file) throws IOException {
//        System.out.println(request.getProtocol());
//        System.out.println(request.getSession());
//        System.out.println(request.getContextPath());
//        System.out.println(request.getMethod());
//        System.out.println(request.getRequestURI());

        InputStream inputStream1 = file.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(new File("request.txt"));

        // 文件保存下来了
        FileOutputStream fileOutputStream1 = new FileOutputStream(new File("request1.txt"));
        byte[] bytes = new byte[1024];
        while (inputStream1.read(bytes) != -1) {
            fileOutputStream1.write(bytes);
        }


        // 文件无内容
        ServletInputStream inputStream = request.getInputStream();

        while (inputStream.read(bytes) != -1) {
            fileOutputStream.write(bytes);
        }
        inputStream.close();
        fileOutputStream.close();


        return JSON.toJSONString("aaa");
    }
    public List<String> getList() {
//        return new ArrayList<>();
        return new LinkedList<>();
    }
}
